#pragma once
#include "Command.h"

#define WM_TRAMPOLINE (WM_USER + 102)

class CImportProgressObserverTrampolineWnd :
	public CWindowImpl<CImportProgressObserverTrampolineWnd, CWindow, CWinTraits<WS_POPUP> >,
	public ImportProgressObserver
{
	ImportProgressObserver *_pObserver;

public:
	explicit CImportProgressObserverTrampolineWnd(ImportProgressObserver *pObserver)
		: _pObserver(pObserver)
	{
	}

BEGIN_MSG_MAP(CImportProgressObserverTrampolineWnd)
	MESSAGE_HANDLER(WM_TRAMPOLINE, OnTrampoline)
END_MSG_MAP()

	LRESULT OnTrampoline(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
	{
		Command *pCommand = reinterpret_cast<Command *>(lParam);
		pCommand->Invoke();
		delete pCommand;	// TODO: Consider using LifestyleTraits or something?

		return 0;
	}

// ImportProgressObserver
public:
	virtual void OnSearchStarted()
	{
		Command *pCommand = new Command0<ImportProgressObserver>(_pObserver, &ImportProgressObserver::OnSearchStarted);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}

	virtual void OnFoundFile(const CString &strPath)
	{
		Command *pCommand = new Command1<ImportProgressObserver, const CString &>(_pObserver, &ImportProgressObserver::OnFoundFile, strPath);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}

	virtual void OnSearchCompleted()
	{
		Command *pCommand = new Command0<ImportProgressObserver>(_pObserver, &ImportProgressObserver::OnSearchCompleted);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}

	virtual void OnImportStarted()
	{
		Command *pCommand = new Command0<ImportProgressObserver>(_pObserver, &ImportProgressObserver::OnImportStarted);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}

	virtual void OnImportFileStarted(const CString &strFilePath, int num, int denom)
	{
		Command *pCommand = new Command3<ImportProgressObserver, const CString &, int, int>(_pObserver, &ImportProgressObserver::OnImportFileStarted,
			strFilePath, num, denom);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}

	virtual void OnDisplayThumbnail(Image *pThumbnail)
	{
		Command *pCommand = new Command1<ImportProgressObserver, Image *>(_pObserver, &ImportProgressObserver::OnDisplayThumbnail, pThumbnail);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}

	virtual void OnImportCompleted()
	{
		Command *pCommand = new Command0<ImportProgressObserver>(_pObserver, &ImportProgressObserver::OnImportCompleted);
		PostMessage(WM_TRAMPOLINE, 0, (LPARAM)pCommand);
	}
};

